var gulp = require('gulp');
var gulpCopy = require('gulp-file-copy');
var stylus = require('gulp-stylus');
var nodemon = require('gulp-nodemon');
var concat = require('gulp-concat');
var minifyCSS = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var jslint = require('gulp-jslint');
var serv = require('gulp-serv');

/**
 *	source path
 */
var src = {
	stylus: './public/css/stylus/*.styl',
	css: ['./public/css/**/*.css', '!./public/css/main.css'],
	js: ['./public/**/*.js', '!./public/js/main.js'],
	controllers: './public/js/controllers/*.js',
}

/**
 *	stylus
 */
gulp.task('compress', function() {
	gulp.src(src.stylus)
		.pipe(stylus({
			compress: true,
		}))
		.pipe(concat('index.css'))
		.pipe(minifyCSS())
		.pipe(gulp.dest('./public/css/'));
});

/**
 *	css concat
 */
gulp.task('css', ['compress'], function() {
	gulp.src(src.css)
		.pipe(concat('main.css'))
		.pipe(minifyCSS())
		.pipe(gulp.dest('./public/css/'));
});

/**
 *	jslint
 */
gulp.task('jshint', function() {
	return gulp.src('js/*.js')
		.pipe(jshint())
		.pipe(jshint.reporter('default'));
});


/**
 *	js uglify and concat
 */
gulp.task('minifyjs', function() {
	return gulp.src(src.js) //需要操作的文件
		.pipe(uglify()) //压缩
		.pipe(concat('main.js')) //合并所有js到main.js
		.pipe(gulp.dest('./public/js/')) //输出到文件夹
});

/**
 *	copy static files
 */
gulp.task('copy', function() {
	var start = src.unhandle;
	gulp.src(start)
		.pipe(gulpCopy('./dist/', {
			start: start
		}))
});

/**
 *	watcher stylus
 */
gulp.watch([src.stylus], ['compress']).on('change', function(event) {
	console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
});

/**
 *	static server
 */
gulp.task("server", function(done) {
  serv.start({
    root: __dirname+"/public",
    port: 8080
  }, done);
});
/**
 *	watcher js
 */
// gulp.watch([src.js], ['minifyjs']).on('change', function(event) {
// 	console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
// });

gulp.task('default', ['server']);